package org.vacoor.nothing.misc.payment.authorize;

import net.authorize.Environment;
import net.authorize.Merchant;
import net.authorize.ResponseReasonCode;
import net.authorize.TransactionType;
import net.authorize.aim.Result;
import net.authorize.aim.Transaction;
import net.authorize.data.creditcard.CreditCard;

import java.io.PrintStream;
import java.math.BigDecimal;

/**
 */
public class AnetDemo {
    public static void main(String[] args) {
        String apiLoginId = "83jN2GYn7m";
        String transactionKey = "58gt6f7W5Y5VG3sD";//"8Y7by6z6L3Pz6Fmm";
        Environment env = Environment.SANDBOX;

        String cno = "370000000000002";
//        String cno = "6011000000000012";
        String month = "12";
        String year = "2020";

        //
        Merchant merchant = Merchant.createMerchant(env, apiLoginId, transactionKey);

        // create credit card
        CreditCard credit = CreditCard.createCreditCard();
        credit.setCreditCardNumber(cno);
        credit.setExpirationMonth(month);
        credit.setExpirationYear(year);

        // create transaction
        Transaction tx = merchant.createAIMTransaction(TransactionType.AUTH_CAPTURE, new BigDecimal(2.99));
        tx.setCreditCard(credit);

        Result<Transaction> result = (Result<Transaction>) merchant.postTransaction(tx);
        PrintStream out = System.out;
        if (result.isApproved()) {   // 批准
            out.println("Approved!");
            out.println("Transaction Id: " + result.getTarget().getTransactionId());
        } else if (result.isDeclined()) {   // 拒绝
            out.println("Declined.");
            out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
        } else {
            out.println("Error.");
            ResponseReasonCode code = result.getReasonResponseCode();
            out.println(result.getReasonResponseCode() + " : " + result.getResponseText());
        }
        // merchant.create
        // System.out.println(result.isReview());

//        merchant.createReportingTransaction()
    }
}
